﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using Controller;

namespace Facade.Validator
{
    public class KPIAchievementValidator
    {
        public string ValidateOnSave(KPIAchievement KPIAchievements)
        {
            string msg = "OK";
            if (KPIAchievements.Realization.Trim().Length <= 0)
                msg = "Please Enter Realization";
            else
            {
                switch (KPIAchievements.KPIAssign.KPI.UoM.Format)
                {
                    case (UoMFormat.Numeric):
                        int number;
                        if (!Int32.TryParse(KPIAchievements.Realization, out number))
                            msg = "Mismatched realization and UoM format";
                        break;
                    case(UoMFormat.Month):
                        IList<KPIAchievement> list = new KPIAchievementController().GetKPIAchievementTable(KPIAchievements.Year, KPIAchievements.KPIAssign.ID);

                        if (list != null && list.Count > 0)
                        {
                            foreach (KPIAchievement e in list)
                            {
                                if (e != null)
                                {
                                    if (e.StatusByUser != null && e.Month != KPIAchievements.Month)
                                    {
                                        msg = "This KPI can be assessed once a year";
                                        break;
                                    }
                                }
                            }
                        }

                        break;
                    default: break;
                }
            }
            if (KPIAchievements.Month <= 0)
                msg = "Please Select Month";
            if (KPIAchievements.Score.Trim().Length <= 0)
                msg = "Please Enter Score";
            return msg;
        }

        public string ValidateOnUpdate(KPIAchievement KPIAchievements)
        {
            string msg = "OK";
            if (KPIAchievements.Realization.Trim().Length <= 0)
                msg = "Please Enter Realization";
            else
            {
                switch (KPIAchievements.KPIAssign.KPI.UoM.Format)
                {
                    case (UoMFormat.Numeric):
                        int number;
                        if (!Int32.TryParse(KPIAchievements.Realization, out number))
                            msg = "Mismatched realization and UoM format";
                        break;
                    case (UoMFormat.Month):
                        IList<KPIAchievement> list = new KPIAchievementController().GetKPIAchievementTable(KPIAchievements.Year, KPIAchievements.KPIAssign.ID);

                        if (list != null && list.Count > 0)
                        {
                            foreach (KPIAchievement e in list)
                            {
                                if (e != null)
                                {
                                    if (e.StatusByUser != null && e.Month != KPIAchievements.Month)
                                    {
                                        msg = "This KPI can be assessed once a year";
                                        break;
                                    }
                                }
                            }
                        }

                        break;
                    default: break;
                }
            }
            if (KPIAchievements.Month <= 0)
                msg = "Please Select Month";
            if (KPIAchievements.Score.Trim().Length <= 0)
                msg = "Please Enter Score";
            return msg;
        }
    }
}